package lxy;

public class ThreadWorkt02 {
    static int i = 0, j = 0;
    static final Object o = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(new T1(),"第一个加法");
        Thread t2 = new Thread(new T1(),"第二个加法");
        Thread t3 = new Thread(new T2(),"第一个减法");
        Thread t4 = new Thread(new T2(),"第二个减法");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

    static class T1 implements Runnable{
        @Override
        public void run() {
            for (int k = 0; k < 10; k++) {
                synchronized(o){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                    j++;
                    System.out.println(Thread.currentThread().getName()+ "i=" + i + "j=" + j);
                }
            }
            }
        }

    static class T2 implements Runnable{
        @Override
        public void run() {
            for (int k = 0; k < 10; k++) {
                synchronized(o){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i--;
                    j--;
                    System.out.println(Thread.currentThread().getName()+ "i=" + i + "j=" + j);
                }
            }
        }
    }
}
